From a2b3383f49530fd4a99e924e8b7973f468795a9d Mon Sep 17 00:00:00 2001 From: Justin Burkett Date: Tue, 29 Nov 2016 21:29:32 -0500 Subject: [PATCH] Quote regexp in -add-key-based-replacements Should fix #154 --- which-key-tests.el | 29 +++++++++++++++++++++++++++++ which-key.el | 4 ++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/which-key-tests.el b/which-key-tests.el index cd055015a19..244992df16a 100644 --- a/which-key-tests.el +++ b/which-key-tests.el @@ -45,5 +45,34 @@ (which-key--maybe-replace '("C-c C-c" . "")) '("C-c C-c" . "complete"))))) +(ert-deftest which-key-test--maybe-replace () + "Test `which-key--maybe-replace'. See #154" + (let ((which-key-replacement-alist + '((("C-c [a-d]" . nil) . ("C-c a" . "c-c a")) + (("C-c .+" . nil) . ("C-c *" . "c-c *"))))) + (which-key-add-key-based-replacements + "C-c ." "test ." + "C-c \\" "regexp quoting" + "C-c [" "bad regexp") + (should (equal + (which-key--maybe-replace '("C-c g" . "test")) + '("C-c *" . "c-c *"))) + (should (equal + (which-key--maybe-replace '("C-c b" . "test")) + '("C-c a" . "c-c a"))) + (should (equal + (which-key--maybe-replace '("C-c ." "not test .")) + '("C-c ." . "test ."))) + (should (not + (equal + (which-key--maybe-replace '("C-c +" "not test .")) + '("C-c ." . "test .")))) + (should (equal + (which-key--maybe-replace '("C-c [" "orig bad regexp")) + '("C-c [" . "bad regexp"))) + (should (equal + (which-key--maybe-replace '("C-c \\" "pre quoting")) + '("C-c \\" . "regexp quoting"))))) + (provide 'which-key-tests) ;;; which-key-tests.el ends here diff --git a/which-key.el b/which-key.el index 496d2cbebd7..7d99fa597a3 100644 --- a/which-key.el +++ b/which-key.el @@ -810,7 +810,7 @@ replacements are added to (while key-sequence ;; normalize key sequences before adding (let ((key-seq (key-description (kbd key-sequence)))) - (push (cons (cons (format "\\`%s\\'" key-seq) nil) + (push (cons (cons (regexp-quote key-seq) nil) (cons nil (or (car-safe replacement) replacement))) which-key-replacement-alist) (when (consp replacement) @@ -836,7 +836,7 @@ addition KEY-SEQUENCE REPLACEMENT pairs) to apply." (while key-sequence ;; normalize key sequences before adding (let ((key-seq (key-description (kbd key-sequence)))) - (push (cons (cons (format "\\`%s\\'" key-seq) nil) + (push (cons (cons (regexp-quote key-seq) nil) (cons nil (or (car-safe replacement) replacement))) mode-alist) (when (consp replacement) -- 2.30.2